package com.webserver.util;

import com.alibaba.druid.pool.DruidDataSource;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * 数据库工具类，维护数据库驱动加载，连接的创建
 */
public class DBUtil {
    /**
     * 连接池：1.重用连接 2.控制连接数量
     */
    private static DruidDataSource druidDataSource;

    static {
        init();
    }

    private static void init() {
        druidDataSource = new DruidDataSource();
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true");
        druidDataSource.setInitialSize(5);
        druidDataSource.setMaxActive(20);
    }

    public static Connection getConnection() throws SQLException {
        return druidDataSource.getConnection();
    }
}
